package com.rock.qrcodelibrary;

import android.app.Activity;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VideoActivity extends Activity {

    MyHandler handler;
    //    ClientThread clientThread;
    ByteArrayOutputStream outstream;

    Button start;
    Button stop;
    SurfaceView surfaceView;
    SurfaceHolder sfh;
    Camera camera;
    boolean isPreview = false;        //是否在浏览中
    int screenWidth = 300, screenHeight = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.camera);

        handler = new MyHandler();
//        clientThread = new ClientThread(handler);
//        new Thread(clientThread).start();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;// 获取屏幕分辨率宽度
        screenHeight = dm.heightPixels;


//        start = (Button)findViewById(R.id.start);
//        stop = (Button)findViewById(R.id.stop);
        surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        sfh = surfaceView.getHolder();
        sfh.setFixedSize(screenWidth, screenHeight / 4 * 3);

        sfh.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                                       int arg3) {
                // TODO Auto-generated method stub
                Log.e("===========", "surfaceChanged: ");

            }

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
                Log.e("===========", "surfaceCreated: ");

                initCamera();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {
                Log.e("===========", "surfaceDestroyed: ");

                if (camera != null) {
                    if (isPreview)
                        camera.setPreviewCallback(null);
                    camera.stopPreview();
                    camera.release();
                    camera = null;
                }

            }

        });

        //开启连接服务
//        start.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//
//                start.setEnabled(false);
//            }
//
//        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main_video, menu);
        return true;
    }

    @SuppressWarnings("deprecation")
    private void initCamera() {
        if (!isPreview) {
            camera = Camera.open();
//            ClientThread.size = camera.getParameters().getPreviewSize();
        }
        if (camera != null && !isPreview) {
            try {
                camera.setPreviewDisplay(sfh);                 // 通过SurfaceView显示取景画面
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(screenWidth, screenHeight / 4 * 3);
                 /* 每秒从摄像头捕获5帧画面， */
                parameters.setPreviewFrameRate(5);
                parameters.setPictureFormat(ImageFormat.NV21);           // 设置图片格式
                parameters.setPictureSize(screenWidth, screenHeight / 4 * 3);    // 设置照片的大小
                camera.setDisplayOrientation(90);
                camera.setPreviewCallback(new Camera.PreviewCallback() {
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + File.separator + "haha.mp4"));

                    @Override
                    public void onPreviewFrame(byte[] data, Camera c) {
//                        // TODO Auto-generated method stub
//                        Camera.Size size = camera.getParameters().getPreviewSize();
//                        Log.e("===size", "onPreviewFrame: " + size);
//                        try {
////                            FileOutputStream fileOutputStream = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + File.separator + "haha.mp4"));
//                            fileOutputStream.write(data);
//
//                            //调用image.compressToJpeg（）将YUV格式图像数据data转为jpg格式
//                            YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
//                            Log.e("===image", "onPreviewFrame: " + image.getYuvData());
//                            if (image != null) {
////                                Message msg = clientThread.revHandler.obtainMessage();
////                                msg.what=0x111;
////                                msg.obj=image;
////                                clientThread.revHandler.sendMessage(msg);
//
//                                    /*outstream = new ByteArrayOutputStream();
//                                    image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, outstream);
//                                    outstream.flush();
//                                    new Thread(clientThread).start();*/
//                            }
//                        } catch (Exception ex) {
//                            Log.e("Sys", "Error:" + ex.getMessage());
//                        }

                        // 刚刚拍照的文件名
                        String fileName = "IMG_"
                                + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
                                .toString() + ".jpg";
                        File sdRoot = Environment.getExternalStorageDirectory();
                        String dir = "/Camera/";
                        File mkDir = new File(sdRoot, dir);
                        if (!mkDir.exists())
                            mkDir.mkdirs();
                        File pictureFile = new File(sdRoot, dir + fileName);
                        if (!pictureFile.exists()) {
                            try {
                                pictureFile.createNewFile();
                                Camera.Parameters parameters = camera.getParameters();
                                Camera.Size size = parameters.getPreviewSize();
                                YuvImage image = new YuvImage(data,
                                        parameters.getPreviewFormat(), size.width, size.height,
                                        null);
                                FileOutputStream filecon = new FileOutputStream(pictureFile);
                                image.compressToJpeg(
                                        new Rect(0, 0, image.getWidth(), image.getHeight()),
                                        90, filecon);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                });
                camera.startPreview();                                   // 开始预览
                camera.autoFocus(null);                                  // 自动对焦
            } catch (Exception e) {
                e.printStackTrace();
            }
            isPreview = true;
        }
    }

    static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x222) {
                //返回信息显示代码
            }
        }
    }
}
